using System;
using System.Web.UI;
using SubversionReports.Entities;
using SubversionReports.Utilities;

namespace SubversionReports.Web.Controls
{
    public partial class PathsView : UserControl
    {
        private Revision revision;

        /// <summary>
        /// Gets or sets the revision.
        /// </summary>
        /// <value>The revision.</value>
        public Revision Revision
        {
            private set
            {
                revision = value;
                RevisionId.Text = revision.Id.ToString();
                RevisionDate.Text = revision.Date.ToString("dddd, dd/MM/yyyy hh:mm:ss tt");
                RevisionAuthor.Text = revision.Author;

                RevisionAuthorIcon.Attributes.Add("style", "float: left; margin: 10px 10px 0 0;");
                RevisionAuthorIcon.ImageUrl = SvnWebDav.GetAuthorIconLargeUrl(SvnWebDav.CurrentRepositoryRootUrl, SvnWebDav.CurrentRepositoryUsername, SvnWebDav.CurrentRepositoryPassword, revision.Author);
                RevisionAuthorIcon.AlternateText = revision.Author;
                RevisionComment.Text = revision.Comment;

                PathRepeater.DataSource = revision.Paths;
                PathRepeater.DataBind();
            }
            get { return revision; }
        }

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if(Visible && SvnWebDav.AuthenticationSuceeded)
            {
                if (string.IsNullOrEmpty(Request["Path"]))
                {
                    PathsViewPlaceHolder.Visible = true;
                    FileViewPlaceHolder.Visible = false;
                    RecordLinks1.RecordCount = SvnWebDav.GetLastRevisionNumber(SvnWebDav.CurrentRepositoryRootUrl, SvnWebDav.CurrentRepositoryUsername, SvnWebDav.CurrentRepositoryPassword);
                    Revision = SvnWebDav.GetRevision(SvnWebDav.CurrentRepositoryRootUrl, SvnWebDav.CurrentRepositoryUsername, SvnWebDav.CurrentRepositoryPassword, int.Parse(Page.Request["RevisionId"] ?? "1"));
                }
                else
                {
                    PathsViewPlaceHolder.Visible = false;
                    FileViewPlaceHolder.Visible = true;
                }
            }
        }

        /// <summary>
        /// Gets the change icon.
        /// </summary>
        /// <param name="revisionType">Type of the revision.</param>
        /// <returns></returns>
        protected static string GetChangeIcon(PathRevisionType revisionType)
        {
            string iconUrl;
            switch(revisionType)
            {
                case PathRevisionType.Added:
                    iconUrl = "Images/added.png";
                    break;
                case PathRevisionType.Modified:
                    iconUrl = "Images/modified.png";
                    break;
                case PathRevisionType.Deleted:
                    iconUrl = "Images/deleted.png";
                    break;
                default:
                    iconUrl = string.Empty;
                    break;
            }
            return iconUrl;
        }
    }
}